package models;

import java.util.LinkedList;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import play.db.jpa.Model;

@Entity
@Table(name = "Application_Roles")
public class ApplicationRole extends Model {

	public String name;

	@ManyToMany(mappedBy = "appRoles")
	public List<UserStory> userStories = new LinkedList<UserStory>();
	// want column at user stories to be application_role_id

	@ManyToOne
	public Project project;

	/**
	 * constructs a new ApplicationRole with a name and a project
	 * 
	 * @param name
	 * @param project
	 */
	public ApplicationRole(String name, Project project) {
		this.name = name;
		this.project = project;
	}

	/**
	 * constructs a new ApplicationRole
	 * 
	 * @param name
	 */
	public ApplicationRole(String name) {
		this.name = name;
	}
	
	
	public ApplicationRole delete()
	{
		for(UserStory story:userStories)
		{
			story.appRoles.remove(this);
			story.save();
		}
		return super.delete();
	}
}
